<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/15/015
 * Time: 14:46
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\AdminOrderInfo;
use app\Lib\Model\DbModel\OrderBindingTag;
use app\Lib\Model\DbModel\OrderInfo;
use app\Lib\Model\Tag\HouseTypeTag\HouseTypeTag;
use app\Lib\Model\Tag\OrderStopWordReason\StopWordTag;
use app\Lib\Model\Tag\StyleTag\StyleTag;
use app\Lib\Model\User\User;

class Order implements \app\Lib\InterfaceIn\Order\Order
{
    private $order_number;
    private $cache;
    private $filed;
    private $Db;
    private $cacheTime = 604800;
    private $cache_prefix = 'order_info_order_number_';
    private $order_status_str = [
        'wait'=>'等待开工',
        'underconstruction'=>'施工中',
        'end'=>'完工',
        'give_up'=>'废弃'
    ];
    public function __construct($order_number, $cache = true)
    {
        $this->cache = $cache;
        $this->order_number = $order_number;
        $this->cache_prefix = $this->cache_prefix.$order_number;
    }

    public function get_order_id()
    {
        $model =  $this->getDb();
        $this->filed = $model->_id;
        return $this->getFiledValue();
    }

    public function get_order_number()
    {
        return $this->order_number;
    }

    public function get_user_name()
    {
        $model =  $this->getDb();
        $this->filed = $model->_user_name;
        return $this->getFiledValue();
    }

    public function get_user_phone()
    {
        $model =  $this->getDb();
        $this->filed = $model->_user_phone;
        return $this->getFiledValue();
    }

    public function get_user_address()
    {
        $model =  $this->getDb();
        $this->filed = $model->_user_address;
        return $this->getFiledValue();
    }

    public function get_order_money()
    {
        $model =  $this->getDb();
        $this->filed = $model->_order_money;
        return $this->getFiledValue();
    }

    public function get_order_style()
    {
        $model =  $this->getDb();
        $this->filed = $model->_order_style;
        return $this->getFiledValue();
    }

    public function get_style_obj(){
        $styleId = $this->get_order_style();
        $model = new StyleTag($styleId);
        return $model;
    }


    public function get_house_type()
    {
        $model =  $this->getDb();
        $this->filed = $model->_house_type;
        return $this->getFiledValue();
    }

    public function get_house_type_obj(){
        $styleId = $this->get_house_type();
        $model = new HouseTypeTag($styleId);
        return $model;
    }

    public function get_design_uid()
    {
        $model =  $this->getDb();
        $this->filed = $model->_design_uid;
        return $this->getFiledValue();
    }

    public function get_design_user()
    {

        return new User($this->get_design_uid());
    }

    public function get_introducer_phone()
    {
        $model =  $this->getDb();
        $this->filed = $model->_introducer_phone;
        return $this->getFiledValue();
    }

    public function get_introducer_phone_user()
    {
        // TODO: Implement get_introducer_phone_user() method.
    }

    public function get_duration()
    {
        $model =  $this->getDb();
        $this->filed = $model->_duration;
        return $this->getFiledValue();
    }

    public function get_create_time()
    {
        $model =  $this->getDb();
        $this->filed = $model->_create_time;
        return $this->getFiledValue();
    }

    public function get_create_time_str()
    {
        return date('Y-m-d H:i:s',$this->get_create_time());
    }

    public function get_create_uid()
    {
        $model =  $this->getDb();
        $this->filed = $model->_create_uid;
        return $this->getFiledValue();
    }

    public function get_create_user()
    {
        return new User($this->get_create_uid());
    }

    public function get_starting_time()
    {
        $model =  $this->getDb();
        $this->filed = $model->_starting_time;
        $result = $this->getFiledValue();
        return $result == '无'?'':$result;
    }

    public function get_starting_time_str()
    {
        $result=  $this->get_starting_time();
        if(!$result){
            return '';
        }
        return   date('Y-m-d H:i:s',$result);
    }

    public function get_site_supervisor_uid()
    {
        $model =  $this->getDb();
        $this->filed = $model->_site_supervisor_uid;
        $user = $this->getFiledValue();
        return $user == '无'?'':$user;
    }

    public function get_site_supervisor_user()
    {
        $uid =  $this->get_site_supervisor_uid();
        if(!$uid){
            return '';
        }
        return new User($uid);
    }

    public function get_orderStatus()
    {
        $model =  $this->getDb();
        $this->filed = $model->_orderStatus;
        return $this->getFiledValue();
    }

    public function get_orderStatus_str()
    {
        $status = $this->get_orderStatus();
        return $this->order_status_str[$status];
    }

    public function get_give_up_reason()
    {
        $model =  $this->getDb();
        $this->filed = $model->_give_up_reason;
        $data = $this->getFiledValue();
        return $data == '无'?'':$data;
    }

    private function getFiledValue(){
        if($this->cache){
            $result = $this->getCache();
        }else{
            $result = $this->getDbFiledValue();
        }
        return $result;
    }


    private function getCache(){
        $cacheName = $this->getCacheName();
        $cacheData = cache($cacheName);
        if(!$cacheData){
            $cacheData = $this->getDbFiledValue();
            cache($cacheName,$cacheData,$this->cacheTime);
        }
        return $cacheData;
    }

    private function getDbFiledValue(){
        $model = $this->getDb();
        $result = $model->where([$model->_order_number=>$this->order_number])->value($this->filed);
//        dump($model->getLastSql());
        return empty($result)?'无':$result;
    }

    private function getDb(){
        if(empty($this->Db)){
            $this->Db = new OrderInfo();
        }
        return $this->Db;
    }

    private function getCacheName(){
        return $this->cache_prefix.'_'.$this->filed;
    }

    /**
     * 删除类的缓存
     * @return bool
     * @throws BlException
     */
    public function delAllCache()
    {
        $model = $this->getDb();
        $result =  $model->getTableFields();
        foreach ($result as $val){
            $this->delSingleCache($val);
        }
        return true;
    }

    /**
     * 删除单个字段缓存
     * @param $field
     * @return bool
     * @throws BlException
     */
    public function delSingleCache($field)
    {
        $this->filed = $field;
        if(empty($field)){
            throw new BlException('请提供要清除缓存的字段');
        }
        $cacheName = $this->getCacheName();
        cache($cacheName,null);
        return true;
    }

    public function is_wait_status()
    {
        if($this->get_orderStatus()!='wait'){
            return false;
        }
        return true;
    }

    public function is_underconstruction_status()
    {
        if($this->get_orderStatus()!='underconstruction'){
            return false;
        }
        return true;
    }

    public function is_end()
    {
        if($this->get_orderStatus()!='end'){
            return false;
        }
        return true;
    }

    public function is_give_up()
    {
        if($this->get_orderStatus()!='give_up'){
            return false;
        }
        return true;
    }

    /**
     * 工单是否存在标签
     * @return mixed
     */
    public function is_hand_stop_word()
    {
        $model = (new OrderBindingTag());
        $map = [
            $model->_order_id=>$this->get_order_id(),
            $model->_status=>1
        ];
        $result = $model->where($map)->value($model->_id);
        return $result?true:false;
    }

    public function getStopWordTag(){
        $model = (new OrderBindingTag());
        $map = [
            $model->_order_id=>$this->get_order_id(),
            $model->_status=>1
        ];
        $result = $model->where($map)->value($model->_tag_id);
        return (new StopWordTag($result));
    }

    public function getStopWordReason(){
        $model = (new OrderBindingTag());
        $map = [
            $model->_order_id=>$this->get_order_id(),
            $model->_status=>1
        ];
        $result = $model->where($map)->value($model->_reason);
        return $result;
    }

    /**
     * 获取工单id
     * @param $orderId
     * @return mixed
     */
    static public function getOrderNumber($orderId){
        $orderInfo = (new OrderInfo());
        return $orderInfo->where(['id'=>$orderId])->value($orderInfo->_order_number);
    }


}